import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from '../../users/entities/user.entity';

@Entity('articles')
export class Article {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 200 })
  title: string;

  @Column({ type: 'varchar', length: 200, unique: true })
  slug: string;

  @Column({ type: 'text', nullable: true })
  excerpt: string; // 摘要

  @Column({ type: 'longtext' })
  content: string; // Markdown 内容

  @Column({ type: 'longtext', nullable: true })
  contentHtml: string; // HTML 内容（渲染后的）

  @Column({ type: 'varchar', length: 255, nullable: true })
  coverImage: string; // 封面图

  @Column({ type: 'int' })
  authorId: number;

  @Column({ type: 'varchar', length: 100, nullable: true })
  category: string; // 文章分类

  @Column({ type: 'simple-array', nullable: true })
  tags: string[]; // 文章标签

  @Column({ type: 'enum', enum: ['draft', 'published', 'archived'], default: 'draft' })
  status: string;

  @Column({ type: 'boolean', default: false })
  isFeatured: boolean; // 是否精选

  @Column({ type: 'int', default: 0 })
  views: number; // 浏览量

  @Column({ type: 'int', default: 0 })
  likes: number; // 点赞数

  // SEO 字段
  @Column({ type: 'varchar', length: 200, nullable: true })
  metaTitle: string; // SEO 标题

  @Column({ type: 'text', nullable: true })
  metaDescription: string; // SEO 描述

  @Column({ type: 'simple-array', nullable: true })
  metaKeywords: string[]; // SEO 关键词

  @Column({ type: 'datetime', nullable: true })
  publishedAt: Date; // 发布时间

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  // 关联
  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'authorId' })
  author: User;
}

